package udpmessaging;

import com.messaging.UdpMq;
import com.messaging.WaitAckMessage;
import com.messaging.events.MessageNotSentEvent;
import java.net.InetAddress;

/**
 * @author sad
 */
public class TestUdpClient {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        UdpMq mq = new UdpMq(5673);
        mq.setMessageNotSentEvent(new MessageNotSentEvent() {

            @Override
            public void failure(WaitAckMessage message) {
                System.err.println("Unprocessed " + UdpMq.copyIntFromArray(message.getPacket().getRealData(), 0));
            }
        });
        mq.start();
        for (int i = 0; i < 10000; i++) {
            byte[] b = new byte[4];
            UdpMq.copyIntToArray(b, i, 0);
            mq.sendMessageReliable(b, InetAddress.getLocalHost(), 9874);
            System.out.println("Sent " + i);
        }

        for (;;) {
            if (mq.waitAckMessagesCount() == 0) {
                break;
            }

            System.out.println("wc=" + mq.waitAckMessagesCount());
            Thread.sleep(1);
        }

        long end = System.currentTimeMillis();
        //Thread.sleep(20000);
        System.out.println("" + (end - start) + "Sended:" + mq.getSendsReliable() + ". Resended:" + mq.getResendsReliable());
        System.exit(0);
    }
}
